package cn.itcast.fore.web.action;

import javax.ws.rs.core.MediaType;

import org.apache.commons.lang3.StringUtils;
import org.apache.cxf.jaxrs.client.WebClient;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;

import cn.itcast.bos.domain.base.Area;
import cn.itcast.bos.domain.take_delivery.Order;
import cn.itcast.bos.utils.Constants;
import cn.itcast.fore.web.action.common.BaseAction;

@Controller
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class OrderAction extends BaseAction<Order> {
	// 属性驱动封装区域信息(省市区)

	private String sendAreaInfo;
	private String recAreaInfo;

	public void setSendAreaInfo(String sendAreaInfo) {
		this.sendAreaInfo = sendAreaInfo;
	}

	public void setRecAreaInfo(String recAreaInfo) {
		this.recAreaInfo = recAreaInfo;
	}

	@Action(value = "order_add", results = { @Result(type = "redirect", location = "/index.html#/order.html") })

	public String add() {
		// 需要处理发件区域和收件区域
		Area sendArea = new Area();// 发件区域
		Area recArea = new Area();// 收件区域
		// 将citypicker组件选中的值进行分割（北京市/北京市/顺义区）
		String[] sendAreaArray = StringUtils.split(sendAreaInfo, "/");
		String[] recAreaArray = StringUtils.split(recAreaInfo, "/");
		// 赋值封装到bean中
		// ---发件区域
		sendArea.setProvince(sendAreaArray[0]);
		sendArea.setCity(sendAreaArray[1]);
		sendArea.setDistrict(sendAreaArray[2]);
		// --收件区域
		recArea.setProvince(recAreaArray[0]);
		recArea.setCity(recAreaArray[1]);
		recArea.setDistrict(recAreaArray[2]);
		// 封装
		model.setSendArea(sendArea);
		model.setRecArea(recArea);
		// 远程调用service
		WebClient.create(Constants.BOS_MANAGEMENT_URL + "/services/orderservice/orders").type(MediaType.APPLICATION_JSON).post(model);

		return SUCCESS;

	}
}
